package com.fand.platform.configuration.validator;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.fand.platform.configuration.dto.DomainNaDto;
import com.fand.platform.configuration.service.ConfigurationService;

/**
 * 域名
 * 
 * @author yaoHT
 * 
 */
@Component(DomainNaValidator.BEAN_ID)
public class DomainNaValidator implements Validator {
	public static final String BEAN_ID = "com.fand.platform.configuration.validator.DomainNaValidator";

	@Qualifier(ConfigurationService.BEAN_ID)
	@Autowired(required = false)
	private ConfigurationService configurationService;

	@Override
	public boolean supports(Class<?> clazz) {
		return DomainNaDto.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		DomainNaDto domainNa = (DomainNaDto) target;

		if (StringUtils.isBlank(domainNa.getCode())) {
			errors.reject("code_required", "编号不能为空");
		} else {
			final int count = this.configurationService.getSiteService().readDomainNaByCodeNqIdForInteger(domainNa);

			if (count > 0) {
				errors.reject("code_not_repeat", "编号不能重复");
			}
		}
	}
}
